<?php
/**
 * 心情管理
 * 
 * @author 	yuanming<yuanming@178.com>
 * @version $Id: signature.php 
 */
class comment_controller_signature extends Controller{
	/**
	 * 
	 *
	 * @var comment_model_state
	 */
	
	public $state;
	public $uid;
	public $pageinfo;
	public $friend_uid;
	public $friend_username;
	private $_module = null;
	
	public function __construct($app, $controller){
		
		parent::__construct($app, $controller);
		$this->state = new comment_model_state();
		$this->_module = $app .'_'.$controller;
		$this->headerView = $this->viewDir."/_header.phtml";
		
	}
	
	//取得状态数据
	public function index_action(){
		
		$this->requireLogin();
		$this->uid = self::$login['uid'];
		$page = _('page',1);
		$page_limit = 10;
		
		list($data, $pageInfo) = MemRow::getPageList($this->_module, $page, $page_limit, array(
			array($this->state, 'getStateListId', array($this->uid)),
			array($this->state, 'getRow')
		));

		$this->pageinfo = $this->genPages($pageInfo, array('uid'=>$this->uid));
		$this->data 	= $data;
		
//		$result = $this->state->read_page_data($this->uid,$page,$page_limit);
//		$this->pageinfo = $this->genPages($result['INFO'], array('uid'=>$this->uid));
		

//		$this->data = $result['ROWS'];
		$this->view = "state_show";
		
	}
	
	
	public function add_state_action(){
		
		$this->_renderJSON();
		$this->requireLogin();
		
		$_POST['uid']		= self::$login['uid'];
		$_POST['username']	= self::$login['nickname'];
		$_POST['state']		=	strip_tags($_POST['state']); 
		$_POST['create_time'] = time();
		$state_data = $this->state->read_a_data($_POST['uid']);
		if($state_data['feel'] == $_POST['feel'] && $state_data['state'] == $_POST['state']){}
		else  $state_id = $this->state->insert_data($_POST);

		if($state_id){
			//增加用户的分组id list cache  行缓存
			MemRow::incrList($state_id, array($this->state, 'getStateListId', array($_POST['uid'])));
	
			//缓存$state_id 详细信息
			$cacheRowKey = MemRow::genRowKey($this->_module, $state_id);
			MemRow::mset($cacheRowKey, array('id' => $state_id) + $_POST);

			if($_POST['feel']) $_POST['state'] = preg_replace($_SERVER['config']['smile2']['searcharray'], $_SERVER['config']['smile2']['replacearray'],$_POST['feel']).$_POST['state'];
			
			$this->ret = self::success(array('uid'=>$_POST['uid'],'data'=>$state_id, 'state'=>$_POST['state'], 'msg'=>'seccess'));
	
			if($_POST['state']){
//					notify_util_log::dolog(self::$login['uid'], 'state', $this->ret);
					feed_util_send::save($_POST['uid'], $_POST['username'], 4, $state_id, array('uid'=>$_POST['uid'],'data'=>$state_id, 'state'=>$_POST['state']));
			}else{
					$this->skipQuestChecking=1;	
			}
		}else{
			$this->ret = self::failure('提交失败！');

		}
	}
	
	public function delete_state_action(){
		
			$this->_renderJSON(); 
			$id = intval(_('quest_id')); 
			$login_uid = self::$login['uid'];
			if ($id && $login_uid) {
				$this->ret = $this->state->delete_data($login_uid,$id);
			}
				
			if($this->ret){
			//删除行缓存
				MemRow::decrList($id, array($this->state, 'getStateListId', array($login_uid)));
				MemRow::removeCache(MemRow::genRowKey($this->_module, $id));			

			}
//			return $this->ret;
			
	}
	
	//得到好友状态
	public function get_allfriend_state_action()
	{
		
		$page		= intval(_('page',1));
		$page_limit = 10;		
		$uid		= self::$login['uid'];
		
		list($data, $pageInfo) = MemRow::getPageList($this->_module, $page, $page_limit, array(
			array($this->state, 'get_friend_new_data_id', array($uid)  ),
			array($this->state, 'getRow')
		));

		$this->pageinfo = $this->genPages($pageInfo, array($uid));
		$this->data 	= $data;
		$this->view 	= 'allfriend_state';
	}
	
	/**
	 * 得到一个所有心情纪录
	 *
	 */
	public function get_afriend_state_action()
	{

		$friend_uid	= intval(_('uid'));
		$page		= _('page',1);	
		$page_limit = 10;
		
		list($data, $pageInfo) = MemRow::getPageList($this->_module, $page, $page_limit, array(
			array($this->state, 'getStateListId', array($friend_uid)),
			array($this->state, 'getRow')
		));

		$this->pageinfo = $this->genPages($pageInfo, array('uid'=>$friend_uid));
		$this->data 	= $data;		

		$this->friend_uid = $friend_uid;
		$this->friend_username = $this->SL->soap_query_info($friend_uid);
		
		if ($friend_uid == self::$login['uid'] ) $this->view = "state_show";
		else $this->view = "afriend_state";

	}
	
	//更新评论条数
	public function update_comment_num_action($id)
	{
		
		$state->update_comment_num($id);
	}
	
	//首页调用
	public function index_show_action()
	{			
		$uid 		= _('uid')?_('uid'):self::$login['uid'];		
		$this->data = $this->state->read_a_data($uid);
		$this->_renderSingle('index_show');

	}
	
	//sms 跳转
	public function state_show_action ()
	{
		$this->get_afriend_state_action();
		
	}


		
}


